package cn.bluedot.tea.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.sun.mail.util.BEncoderStream;

import cn.bluedot.framework.annotations.Dispatcher;
import cn.bluedot.framework.annotations.Redirect;
import cn.bluedot.framework.annotations.RequestView;
import cn.bluedot.framework.annotations.ResponseFile;
import cn.bluedot.framework.annotations.ResponseJson;
import cn.bluedot.framework.annotations.Service;
import cn.bluedot.framework.service.BaseService;
import cn.bluedot.framework.util.TimeUtil;
import cn.bluedot.framework.web.Result;
import cn.bluedot.tea.dao.BaseDao;
import cn.bluedot.tea.pojo.domain.Device;
import cn.bluedot.tea.pojo.domain.DeviceModel;
import cn.bluedot.tea.pojo.domain.DeviceRepair;
import cn.bluedot.tea.pojo.domain.PutAndDelivery;
import cn.bluedot.tea.pojo.domain.Supplier;
import cn.bluedot.tea.pojo.domain.Tea;
import cn.bluedot.tea.pojo.domain.TeaCategory;
import cn.bluedot.tea.pojo.domain.User;
import cn.bluedot.tea.pojo.vo.DeviceVO;
import cn.bluedot.tea.pojo.vo.PutAndDeliveryVO;
import cn.bluedot.tea.pojo.vo.TeaVO;
import cn.bluedot.tea.util.BeanUtil;
import cn.bluedot.tea.util.ExcelUtil;
import cn.bluedot.tea.util.Page;
import cn.bluedot.tea.util.PageBean;

@Service
public class RepositoryService extends BaseService {

    // 无参构造函数
    public RepositoryService() {}


    public RepositoryService(String uri, Map<String, Object[]> params, Map<String, Object> data) {
        // 调用父类有参构造
        super(params, data);
        // 执行业务, 调用父类方法
        execute(uri, params, data);
    }

    /**
     * @param params
     * @param data
     *	 仓库管理用例： 新增茶叶人库
     * @throws Exception 
     */
    @RequestView(uri="/teaPut", view="")
    @Redirect(success="/toTea.do", failed = "")
    private void teaPut(Map<String, Object[]> params, Map<String, Object> data) throws Exception {
    	// 从前端传来的参数都是合法验证的
    	Map<String, Object> param = getParameterMap(params);
    	param.put("buyTime",TimeUtil.formatTimeByDefault());
    	Map<String, Object> temp = new HashMap<String, Object>();
    	temp.put("view", "selectTeaCategoryById");
    	temp.put("teaId", param.get("teaId"));
    	TeaCategory teaCategory = (TeaCategory) BaseDao.excuteQuery(temp, TeaCategory.class);
    	String teaName = teaCategory.getTeaName();
    	param.put("name", teaName);
    	param.put("batch", param.get("buyBatch"));
    	param.put("repertory", param.get("buyNumber"));
    	// 先登记茶叶
    	param.put("view", "teaRegister");
    	BaseDao.excute(param);
    	// 然后入库茶叶
    	param.put("view", "teaPut");
    	BaseDao.excute(param);
    	
    	data.put("RESULT", Result.SUCCESS);
    }
    
    
    @RequestView(uri="/searchTea", view="searchTea")
    @Dispatcher(success="/repertory2/tea", failed="")
    private void searchTea(Map<String, Object[]> params, Map<String, Object> data) throws Exception{
    	Map<String, Object> param = getParameterMap(params);
    	
    	// 获取当前页码
    	Integer num = (Integer) param.get("pagenum");
    	Integer pagenum = num == null ? 1 : num;
    	
    	// 分页初始化
    	Page.startPage(pagenum, 10);
    	
    	List<PutAndDelivery> putAndDeliverys = (List<PutAndDelivery>) BaseDao.excuteQuery(param, PutAndDelivery.class, true);
    	// 查询出入库记录
    	List<PutAndDeliveryVO>  putAndDeliveryVOs = new ArrayList<PutAndDeliveryVO>();
    	Map<String, Object> getUserById = new HashMap<String, Object>();
    	Map<String, Object> temp = new HashMap<String, Object>();
    	
    	PutAndDeliveryVO putAndDeliveryVO = null;
    	for(int i = 0; i<putAndDeliverys.size();i++) {
    		putAndDeliveryVO = BeanUtil.convertBean(putAndDeliverys.get(i), PutAndDeliveryVO.class);
    		// 查询出我们的操作员信息
    		getUserById.put("userId", putAndDeliverys.get(i).getUserId());
    		System.out.println(putAndDeliverys.get(i).getUserId());
    		getUserById.put("view", "getUserById");
    		User user = (User) BaseDao.excuteQuery(getUserById, User.class);
    		putAndDeliveryVO.setUser(user);
    		putAndDeliveryVOs.add(putAndDeliveryVO);
    	}
    	
    	// 查询所有茶种
    	List<TeaCategory> teaCategories = new ArrayList<TeaCategory>();
    	temp.clear();
    	temp.put("view", "selectAllTeaCategory");
    	teaCategories = (List<TeaCategory>) BaseDao.excuteQuery(temp, TeaCategory.class);
    	
    	// 查询所有供应商
    	List<Supplier> suppliers = new ArrayList<Supplier>();
    	temp.clear();
    	temp.put("view", "selectAllSupplier");
    	suppliers = (List<Supplier>) BaseDao.excuteQuery(temp, Supplier.class);
    	
    	// 封装PageBean
    	PageBean<?> pageBean = new PageBean(putAndDeliveryVOs, 10);
    	data.put("putAndDeliveryVOs", pageBean);
    	data.put("teaCategories", teaCategories);
    	data.put("suppliers", suppliers);
    	
    	// 获取出库列表
    	Map<String, PutAndDeliveryVO> temporaryMap = new HashMap<String, PutAndDeliveryVO>();
    	putAndDeliveryVOs.forEach(item -> {
    		if(temporaryMap.get(item.getBuyBatch()) == null) {
    			// 说明是第一次， 那么就加入
    			temporaryMap.put(item.getBuyBatch(), item);
    		}else {
    			// 如果是后几个, 比较时间，后者优先
    			PutAndDeliveryVO tempPutAndDelivery = temporaryMap.get(item.getBuyBatch());
    			if(TimeUtil.compareTime(item.getPdTime(), tempPutAndDelivery.getPdTime()) == 1) {
    				temporaryMap.put(item.getBuyBatch(), item);
    			}
    		}
    	});
    	
    	List<PutAndDeliveryVO> list = new ArrayList<PutAndDeliveryVO>();
    	temporaryMap.forEach((key, value) -> {
    		if(value.getRepertory() > 0) {
    			list.add(value);
    		}
    	});
    	
    	data.put("putAndDeliveryVOList", list);
    	
    	data.put("RESULT", Result.SUCCESS);
    	
    }
    
    @RequestView(uri="/searchDevice", view="searchDevice")
    @Dispatcher(success="/repertory2/device", failed="")
    private void searchDevice(Map<String, Object[]> params, Map<String, Object> data) throws Exception {
    	Map<String, Object> param = getParameterMap(params);
    	
    	// 获取当前页码
    	Integer num = (Integer) param.get("pagenum");
    	Integer pagenum = num == null ? 1 : num;
    	
    	param.put("1", "%" + param.get("1").toString() + "%");
    	// 分页初始化
    	Page.startPage(pagenum, 10);
    	List<PutAndDelivery> putAndDeliverys = (List<PutAndDelivery>) BaseDao.excuteQuery(param, PutAndDelivery.class, true);
    	putAndDeliverys.forEach(System.out::println);
    	// 查询出入库记录
    	List<PutAndDeliveryVO>  putAndDeliveryVOs = new ArrayList<PutAndDeliveryVO>();
    	Map<String, Object> getUserById = new HashMap<String, Object>();
    	Map<String, Object> temp = new HashMap<String, Object>();
    	
    	PutAndDeliveryVO putAndDeliveryVO = null;
    	for(int i = 0; i<putAndDeliverys.size();i++) {
    		putAndDeliveryVO = BeanUtil.convertBean(putAndDeliverys.get(i), PutAndDeliveryVO.class);
    		// 查询出我们的操作员信息
    		getUserById.put("userId", putAndDeliverys.get(i).getUserId());
    		System.out.println(putAndDeliverys.get(i).getUserId());
    		getUserById.put("view", "getUserById");
    		User user = (User) BaseDao.excuteQuery(getUserById, User.class);
    		putAndDeliveryVO.setUser(user);
    		putAndDeliveryVOs.add(putAndDeliveryVO);
    	}
    	// 获取所有的设备型号
    	temp.clear();
    	temp.put("view", "selectAllDeviceModel");
    	List<DeviceModel> deviceModels = (List<DeviceModel>) BaseDao.excuteQuery(temp, DeviceModel.class);
//    	deviceModels.forEach(System.out::println);
    	// 获取所有的供应商
    	temp.clear();
    	List<Supplier> suppliers;
    	temp.put("view", "selectAllSupplier");
    	suppliers = (List<Supplier>) BaseDao.excuteQuery(temp, Supplier.class);
    	
    	temp.put("view", "selectAllDevice");
    	List<Device> devices = (List<Device>) BaseDao.excuteQuery(temp, Device.class);
    	// 获取所有可以出库的设备
    	List<String> devicePuts = new ArrayList<String>();
    	devices.forEach(device -> {
    		if(device.getIsPut() == 1) {
    			devicePuts.add(device.getDeviceId());
    		}
    	});
    	PageBean<?> pageBean = new PageBean<>(putAndDeliveryVOs, 10);
    	data.put("devicePuts", devicePuts);
    	data.put("deviceModels", deviceModels);
    	data.put("putAndDeliveryVOs", pageBean);

    	data.put("suppliers", suppliers);
    	data.put("RESULT", Result.SUCCESS);
    }
    
    // 去茶叶出入库页面
    @RequestView(uri="/toTea", view="")
    @Dispatcher(success="/repertory2/tea", failed="")
    private void toTea(Map<String, Object[]> params, Map<String,Object> data) throws Exception {
    	Map<String, Object> param = getParameterMap(params);
    	// 获取当前页码
    	String num = (String) param.get("pagenum");
		param.remove("pagenum");
		int pagenum = num == null ? 1 : Integer.parseInt(num);
    	
    	// 分页初始化
    	Page.startPage(pagenum, 10);
    	// 查询出出入库记录
    	Map<String, Object> temp = new HashMap<String, Object>();
    	temp.put("view", "selectAllTeaPutAndDelivery");
    	temp.put("type", 0);
    	List<PutAndDelivery> putAndDeliverys = (List<PutAndDelivery>) BaseDao.excuteQuery(temp, PutAndDelivery.class);
    	
    	// 查询出入库记录
    	List<PutAndDeliveryVO>  putAndDeliveryVOs = new ArrayList<PutAndDeliveryVO>();
    	Map<String, Object> getUserById = new HashMap<String, Object>();
    	
    	
    	PutAndDeliveryVO putAndDeliveryVO = null;
    	for(int i = 0; i<putAndDeliverys.size();i++) {
    		putAndDeliveryVO = BeanUtil.convertBean(putAndDeliverys.get(i), PutAndDeliveryVO.class);
    		// 查询出我们的操作员信息
    		getUserById.put("userId", putAndDeliverys.get(i).getUserId());
    		getUserById.put("view", "getUserById");
    		//System.out.println(getUserById.get("view"));
    		User user = (User) BaseDao.excuteQuery(getUserById, User.class);
    		putAndDeliveryVO.setUser(user);
    		putAndDeliveryVOs.add(putAndDeliveryVO);
    	}
    	
    	// 查询所有茶种
    	List<TeaCategory> teaCategories = new ArrayList<TeaCategory>();
    	temp.clear();
    	temp.put("view", "selectAllTeaCategory");
    	teaCategories = (List<TeaCategory>) BaseDao.excuteQuery(temp, TeaCategory.class);
    	
    	// 查询所有供应商
    	List<Supplier> suppliers = new ArrayList<Supplier>();
    	temp.clear();
    	temp.put("view", "selectAllSupplier");
    	suppliers = (List<Supplier>) BaseDao.excuteQuery(temp, Supplier.class);
    	
    	// 封装PageBean
    	PageBean<?> pageBean = new PageBean(putAndDeliveryVOs, 10);
    	data.put("putAndDeliveryVOs", pageBean);
    	data.put("teaCategories", teaCategories);
    	data.put("suppliers", suppliers);
    	
    	// 获取出库列表
    	Map<String, PutAndDeliveryVO> temporaryMap = new HashMap<String, PutAndDeliveryVO>();
    	putAndDeliveryVOs.forEach(item -> {
    		if(temporaryMap.get(item.getBuyBatch()) == null) {
    			// 说明是第一次， 那么就加入
    			temporaryMap.put(item.getBuyBatch(), item);
    		}else {
    			// 如果是后几个, 比较时间，后者优先
    			PutAndDeliveryVO tempPutAndDelivery = temporaryMap.get(item.getBuyBatch());
    			if(TimeUtil.compareTime(item.getPdTime(), tempPutAndDelivery.getPdTime()) == 1) {
    				temporaryMap.put(item.getBuyBatch(), item);
    			}
    		}
    	});
    	
    	List<PutAndDeliveryVO> list = new ArrayList<PutAndDeliveryVO>();
    	temporaryMap.forEach((key, value) -> {
    		if(value.getRepertory() > 0) {
    			list.add(value);
    		}
    	});
    	
    	data.put("putAndDeliveryVOList", list);
    	
    	data.put("RESULT", Result.SUCCESS);
    }
    
    
    @RequestView(uri="/teaDetail", view="teaDetail")
    @Dispatcher(success="/repertory2/teaDetail", failed="")
    private void teaDetail(Map<String, Object[]> params, Map<String, Object> data) throws Exception {
    	Map<String, Object> param = getParameterMap(params);
    	Tea tea = (Tea) BaseDao.excuteQuery(param, Tea.class);
    	if(tea != null) {
    		// 进行封装
        	TeaVO teaVO = BeanUtil.convertBean(tea, TeaVO.class);
        	Map<String, Object> temp = new HashMap<String, Object>();
        	temp.put("view", "selectTeaCategoryById");
        	temp.put("teaId", tea.getTeaId());
        	TeaCategory teaCategory = (TeaCategory) BaseDao.excuteQuery(temp, TeaCategory.class);
        	teaVO.setTeaCategory(teaCategory);
        	
        	temp.clear();
        	temp.put("view", "getSupplierById");
        	temp.put("supplierId", tea.getSupplierId());
        	Supplier supplier = (Supplier) BaseDao.excuteQuery(temp, Supplier.class);
        	teaVO.setSupplier(supplier);
        	
        	data.put("teaVO", teaVO);
        	data.put("RESULT", Result.SUCCESS);
    	}
    	data.put("RESULT", Result.SUCCESS);
    	
    	
    }

    /**
     * @param params
     * @param data
     *	 仓库管理用例： 新增茶叶出库
     * @throws Exception 
     */
    @RequestView(uri="/teaDelivery", view="teaDelivery")
    @Redirect(success="/toTea.do", failed = "")
    private void teaDelivery(Map<String, Object[]> params, Map<String, Object> data) throws Exception {
    	Map<String, Object> param = getParameterMap(params);
    	// 生成出库编号(查询出当天的出库批次)
    	Map<String, Object> temp = new HashMap<String, Object>();
    	temp.put("view", "selectCountByBatchLike");
    	temp.put("like", "K" + TimeUtil.formatTimeByYyyyMMddFormat() + "%");
    	int size = (int) BaseDao.excuteQuery(temp, null);
    	String batch = "K" + TimeUtil.formatTimeByYyyyMMddFormat() + String.format("%02d", (size+1));
    	param.put("batch", batch);
    	// 根据BuyBatch 获取基本信息进行填充
    	temp.put("view", "selectPutAndDeliveryByBuyBatch");
    	temp.put("buyBatch", param.get("buyBatch"));
    	List<PutAndDelivery> putAndDeliverys = (List<PutAndDelivery>) BaseDao.excuteQuery(temp, PutAndDelivery.class, true);
    	// 从里面找到最新的一条数据
    	PutAndDelivery end = putAndDeliverys.get(0);
    	for(int i = 1; i < putAndDeliverys.size(); i++) {
    		System.out.println(putAndDeliverys.get(i));
    		if(TimeUtil.compareTime(end.getPdTime(), putAndDeliverys.get(i).getPdTime()) < 0) {
    			end = putAndDeliverys.get(i);
    		}
    	}
    	// 找到最新数据进行填充
    	param.put("name", end.getName());
    	param.put("pdTime", TimeUtil.formatTimeByDefault());
    	// 这里要进行判断， 判断库存是否有出库的多，否则无法出库
    	Double number = Double.parseDouble(param.get("number").toString());
    	if(end.getRepertory() > number) {
    		param.put("repertory", end.getRepertory() - number);
    	}else {
    		// 无法出库
    	}
    	
    	// 下面就是直接操作数据库插入数据
    	// param.forEach((key, value) -> System.out.println("key : " + key + " value : " + value));
    	BaseDao.excute(param);
    	data.put("RESULT", Result.SUCCESS);
    	
    }
    
    
    @RequestView(uri="/toDevice", view="toDevice")
    @Dispatcher(success="/repertory2/device", failed="")
    private void toDevice(Map<String, Object[]> params, Map<String, Object> data) throws Exception {
    	Map<String, Object> param = getParameterMap(params);
    	// 获取当前页码
    	String num = (String) param.get("pagenum");
		param.remove("pagenum");
		int pagenum = num == null ? 1 : Integer.parseInt(num);
    	
    	// 分页初始化
    	Page.startPage(pagenum, 10);
    	Map<String, Object> temp = new HashMap<String, Object>();
    	param.put("type", 1);
    	List<PutAndDelivery> putAndDeliverys = (List<PutAndDelivery>) BaseDao.excuteQuery(param, PutAndDelivery.class);
    	
    	// 查询出入库记录
    	List<PutAndDeliveryVO>  putAndDeliveryVOs = new ArrayList<PutAndDeliveryVO>();
    	Map<String, Object> getUserById = new HashMap<String, Object>();
    	
    	
    	PutAndDeliveryVO putAndDeliveryVO = null;
    	for(int i = 0; i<putAndDeliverys.size();i++) {
    		putAndDeliveryVO = BeanUtil.convertBean(putAndDeliverys.get(i), PutAndDeliveryVO.class);
    		// 查询出我们的操作员信息
    		getUserById.put("userId", putAndDeliverys.get(i).getUserId());
    		getUserById.put("view", "getUserById");
    		//System.out.println(getUserById.get("view"));
    		User user = (User) BaseDao.excuteQuery(getUserById, User.class);
    		putAndDeliveryVO.setUser(user);
    		putAndDeliveryVOs.add(putAndDeliveryVO);
    	}

    	
    	// 获取所有的设备型号
    	temp.clear();
    	temp.put("view", "selectAllDeviceModel");
    	List<DeviceModel> deviceModels = (List<DeviceModel>) BaseDao.excuteQuery(temp, DeviceModel.class);
//    	deviceModels.forEach(System.out::println);
    	// 获取所有的供应商
    	temp.clear();
    	List<Supplier> suppliers;
    	temp.put("view", "selectAllSupplier");
    	suppliers = (List<Supplier>) BaseDao.excuteQuery(temp, Supplier.class);
    	
    	temp.put("view", "selectAllDevice");
    	List<Device> devices = (List<Device>) BaseDao.excuteQuery(temp, Device.class);
    	// 获取所有可以出库的设备
    	List<String> devicePuts = new ArrayList<String>();
    	devices.forEach(device -> {
    		if(device.getIsPut() == 1) {
    			devicePuts.add(device.getDeviceId());
    		}
    	});
    	PageBean<?> pageBean = new PageBean<>(putAndDeliveryVOs, 10);
    	data.put("devicePuts", devicePuts);
    	data.put("deviceModels", deviceModels);
    	data.put("putAndDeliveryVOs", pageBean);

    	data.put("suppliers", suppliers);
    	data.put("RESULT", Result.SUCCESS);
    	
    }

    /**
     * @param params
     * @param data
     * 仓库管理用例： 新增设备出入库
     * @throws Exception 
     */
    @RequestView(uri="/devicePut", view="devicePut")
    @Redirect(success="/toDevice.do", failed = "")
    private void devicePut(Map<String, Object[]> params, Map<String, Object> data) throws Exception {
    	Map<String, Object> param = getParameterMap(params);
    	// 采购批次(不能自动生成，因为会多次插入)和设备编号自动生成
    	// 生成设备编号(查询出当天的出库批次)
    	Map<String, Object> temp = new HashMap<String, Object>();
    	temp.put("view", "selectCountByBatchLike");
    	temp.put("like", "D" + TimeUtil.formatTimeByYyyyMMddFormat() + "%");
    	int size = (int) BaseDao.excuteQuery(temp, null);
    	String deviceId = "D" + TimeUtil.formatTimeByYyyyMMddFormat() + String.format("%02d", (size+1));
    	param.put("deviceId", deviceId);
    	param.put("pdTime", TimeUtil.formatTimeByDefault());
    	param.put("number", 1);
    	param.put("repertory", 1);
    	param.put("type", 1);
    	param.put("pdType", 1);
    	BaseDao.excute(param);
    	
    	// 设备登记
    	param.put("view", "deviceRegister");
    	BaseDao.excute(param);
    	data.put("RESULT", Result.SUCCESS);
    }

    /**
     * @param params
     * @param data
     * 	仓库管理用例： 新增设备出库
     * @throws Exception 
     */
    @RequestView(uri="/deviceDelivery", view="deviceDelivery")
    @Redirect(success="/toDevice.do", failed = "")
    private void deviceDelivery(Map<String, Object[]> params, Map<String, Object> data) throws Exception {
    	Map<String, Object> param = getParameterMap(params);
    	Map<String, Object> temp = new HashMap<String, Object>();
    	temp.put("view", "updateDeviceById");
    	temp.put("isPut", 0);
    	temp.put("deviceId", param.get("deviceId"));
    	BaseDao.excute(temp);
    	//  根据ID查找到我们的情况
    	temp.clear();
    	temp.put("view", "selectPutAndDeliveryByBatch");
    	temp.put("deviceId", param.get("deviceId"));
    	PutAndDelivery putAndDelivery = (PutAndDelivery) BaseDao.excuteQuery(temp, PutAndDelivery.class);
    	// 进行填充
    	param.put("buyBatch", putAndDelivery.getBuyBatch());
    	param.put("number", 1);
    	param.put("repertory", putAndDelivery.getRepertory() - 1);
    	param.put("pdTime", TimeUtil.formatTimeByDefault());
    	param.put("name", putAndDelivery.getName());
    	// 出库是0， 入库是1
    	param.put("pdType", 0);
    	// 设备是1， 茶叶是0
    	param.put("type", 1);
    	BaseDao.excute(param);
    	
    	data.put("RESULT", Result.SUCCESS);
    }
    
    @RequestView(uri="/deviceDetail", view="deviceDetail")
    @Dispatcher(success="/repertory2/deviceDetail", failed="")
    private void deviceDetail(Map<String, Object[]> params, Map<String, Object> data) throws Exception {
    	Map<String, Object> param = getParameterMap(params);
    	Device device = (Device) BaseDao.excuteQuery(param, Device.class);
    	Map<String, Object> temp = new HashMap<>();
    	if(device != null) {
    		// 进行封装成DeviceVO
        	DeviceVO deviceVO = BeanUtil.convertBean(device, DeviceVO.class);
        	temp.put("view", "getDeviceModelById");
        	temp.put("deviceModel", device.getDeviceModel());
        	DeviceModel deviceModel = (DeviceModel) BaseDao.excuteQuery(temp, DeviceModel.class);
        	deviceVO.setModel(deviceModel);
        	temp.clear();
        	temp.put("view", "getSupplierById");
        	temp.put("supplierId", device.getSupplierId());
        	Supplier supplier = (Supplier) BaseDao.excuteQuery(temp, Supplier.class);
        	deviceVO.setSupplier(supplier);
        	
        	data.put("RESULT", Result.SUCCESS);
        	data.put("deviceVO", deviceVO);
        	
    	}
    	
    	
    	
    }

    /**
     * @param params
     * @param data
     *	 仓库管理： 新增茶种
     * @throws Exception 
     */
    @RequestView(uri="/addTeaCategory", view="addTeaCategory")
    @Redirect(success="/toTeaCategory.do", failed="")
    private void addTeaCategory(Map<String, Object[]> params, Map<String, Object> data) throws Exception {
    	Map<String, Object> param = getParameterMap(params);
    	BaseDao.excute(param);
    	data.put("RESULT", Result.SUCCESS);
    }

//    /**
//     * @param params
//     * @param data
//     * 仓库管理： 修改茶种
//     */
//    @RequestView(uri="/updateTeaCategory", view="updateTeaCategory")
//    @Dispatcher(success="", failed = "")
//    private void updateTeaCategory(Map<String, Object[]> params, Map<String, Object> data) {
//
//    }
    
    @RequestView(uri="/searchTeaCategory", view="searchTeaCategory")
    @Dispatcher(success="/info/tea_category", failed="")
    private void searchTeaCategory(Map<String, Object[]> params, Map<String, Object> data) throws Exception {
    	Map<String, Object> param = getParameterMap(params);
    	List<TeaCategory> teaCategories = (List<TeaCategory>) BaseDao.excuteQuery(param, TeaCategory.class, true);
    	Map<String, Object> temp = new HashMap<String, Object>();
    	temp.put("view", "searchTeaClass");
    	List<String> teaClassSet = (List<String>) BaseDao.excuteQuery(temp, String.class);
    	teaClassSet.forEach(System.out::println);
    	data.put("teaCategories", teaCategories);
    	data.put("teaClassSet", teaClassSet);
    	data.put("RESULT", Result.SUCCESS);
    }

    /**
     * @param params
     * @param data
     * 	仓库管理：新增设备型号
     * @throws Exception 
     */
    @RequestView(uri="/addDeviceModel", view="addDeviceModel")
    @Redirect(success="/toDeviceModel.do", failed="")
    private void addDeviceModel(Map<String, Object[]> params, Map<String, Object> data) throws Exception {
    	Map<String, Object> param = getParameterMap(params);
    	BaseDao.excute(param);
    	data.put("RESULT", Result.SUCCESS);
    }

//    /**
//     *
//     * @param params
//     * @param data
//     *	 仓库管理：修改设备型号
//     */
//    @RequestView(uri="/updateDeviceModel", view="updateDeviceModel")
//    @Dispatcher(success="", failed = "")
//    private void updateDeviceModel(Map<String, Object[]> params, Map<String, Object> data) {
//    	
//    }

    /**
     * @param params
     * @param data
     * 	仓库管理：新增供应商
     * @throws Exception 
     */
    @RequestView(uri="/addSupplier", view="addSupplier")
    @Redirect(success="/toSupplier.do", failed = "")
    private void addSupplier(Map<String, Object[]> params, Map<String, Object> data) throws Exception {
    	Map<String, Object> param = getParameterMap(params);
    	BaseDao.excute(param);
    	data.put("RESULT", Result.SUCCESS);
    }

//    /**
//     * @param params
//     * @param data
//     * 	仓库管理：修改供应商
//     */
//    @RequestView(uri="/updateSupplier", view="updateSupplier")
//    @Dispatcher(success="", failed = "")
//    private void updateSupplier(Map<String, Object[]> params, Map<String, Object> data) {
//
//    }
    
    /**
     * 	根据条件搜索供应者
     * @param params
     * @param data
     * @throws Exception
     */
    @RequestView(uri="/searchSupplier", view="searchSupplier")
    @Dispatcher(success="/info/supplier", failed="")
    private void searchSupplier(Map<String, Object[]> params, Map<String, Object> data) throws Exception {
    	Map<String, Object> param = getParameterMap(params);
    	List<Supplier> suppliers = (List<Supplier>) BaseDao.excuteQuery(param, Supplier.class, true);
    	data.put("suppliers", suppliers);
    	data.put("RESULT", Result.SUCCESS);
    }
    
    
    @RequestView(uri="/toDeviceRepair", view="toDeviceRepair")
    @Dispatcher(success="/repertory2/repair", failed="")
    private void toDeviceRepair(Map<String, Object[]> params, Map<String, Object> data) throws Exception {
    	Map<String, Object> param = getParameterMap(params);
    	List<DeviceRepair> deviceRepairs = (List<DeviceRepair>) BaseDao.excuteQuery(param, DeviceRepair.class);
    	data.put("deviceRepairs", deviceRepairs);
    	data.put("RESULT", Result.SUCCESS);
    }

    /**
     * @param params
     * @param data
     *	 仓库管理：新增设备维修
     * @throws Exception 
     */
    @RequestView(uri="/addDeviceRepair", view="addDeviceRepair")
    @Redirect(success="/toDeviceRepair.do", failed = "")
    private void addDeviceRepair(Map<String, Object[]> params, Map<String, Object> data) throws Exception{
    	Map<String, Object> param = getParameterMap(params);
    	// 先检验该设备编号是否存在
    	String deviceId = param.get("deviceId").toString();
    	Map<String, Object> temp = new HashMap<String, Object>();
    	temp.put("view", "selectDeviceById");
    	temp.put("deviceId", param.get("deviceId"));
    	Device device = (Device) BaseDao.excuteQuery(temp, Device.class);
    	if(device != null) {
    		param.put("time", TimeUtil.formatTimeByDefault());
    		// 说明存在， 那么就进行插入
    		BaseDao.excute(param);
    	}else {
    		// 不存在，报错
    	}
    	data.put("RESULT", Result.SUCCESS);
    }
    
    /**
     * 
     * @param params
     * @param data
     * @throws Exception
     */
    @RequestView(uri="/searchDeviceRepair", view="searchDeviceRepair")
    @Dispatcher(success="/repertory2/repair", failed="")
    private void searchDeviceRepair(Map<String, Object[]> params, Map<String, Object> data) throws Exception {
    	Map<String, Object> param = getParameterMap(params);
    	List<DeviceRepair> deviceRepairs = (List<DeviceRepair>) BaseDao.excuteQuery(param, DeviceRepair.class, true);
    	data.put("deviceRepairs", deviceRepairs);
    	data.put("RESULT", Result.SUCCESS);
    }

//    /**
//     * @param params
//     * @param data
//     * 	仓库管理：修改设备维修
//     */
//    @RequestView(uri="/updateDeviceRepair", view="updateDeviceRepair")
//    @Dispatcher(success="", failed = "")
//    private void updateDeviceRepair(Map<String, Object[]> params, Map<String, Object> data) {
//
//    }
    
    // 去茶种信息页面
    @RequestView(uri="/toTeaCategory", view="toTeaCategory")
    @Dispatcher(success="/info/tea_category", failed="")
    private void toTeaCategory(Map<String, Object[]> params, Map<String, Object> data) throws Exception {
    	Map<String,Object> param = getParameterMap(params);
    	List<TeaCategory> teaCategories = (List<TeaCategory>) BaseDao.excuteQuery(param, TeaCategory.class);
    	data.put("teaCategories", teaCategories);
    	// 从所有茶种中找到我们所含有的茶叶系别
    	Set<String> teaClassSet = teaCategories.stream().map(item -> item.getTeaClass()).collect(Collectors.toSet());
//    	teaCategories.forEach(System.out::println);
    	data.put("teaClassSet", teaClassSet);
    	teaClassSet.forEach(System.out::println);
    	data.put("RESULT", Result.SUCCESS);
    }
    
    @RequestView(uri="/toTeaCategoryEdit", view="toTeaCategoryEdit")
    @Dispatcher(success="/info/tea_category_edit", failed="")
    private void toTeaCategoryEdit(Map<String, Object[]> params, Map<String, Object> data) throws Exception {
    	Map<String, Object> param = getParameterMap(params);
    	// 根据ID获取信息
    	TeaCategory teaCategory = (TeaCategory) BaseDao.excuteQuery(param, TeaCategory.class);
    	data.put("RESULT", Result.SUCCESS);
    	data.put("teaCategory", teaCategory);
    }
    
    @RequestView(uri="/editTeaCategory", view="editTeaCategory")
    @ResponseJson
    private void teaCategoryEdit(Map<String, Object[]> params, Map<String, Object> data) throws Exception {
    	Map<String, Object> param = getParameterMap(params);
    	BaseDao.excute(param);
    	data.put("RESULT", Result.SUCCESS);
    	data.put("message", "修改成功");
    	
    }
    
    // 去设备型号页面
    @RequestView(uri="/toDeviceModel", view="toDeviceModel")
    @Dispatcher(success="/info/device_model", failed="")
    private void toDeviceModel(Map<String, Object[]> params, Map<String, Object> data) throws Exception {
    	Map<String, Object> param = getParameterMap(params);
    	List<DeviceModel> deviceModels = (List<DeviceModel>) BaseDao.excuteQuery(param, DeviceModel.class);
    	data.put("deviceModels", deviceModels);
    	deviceModels.forEach(System.out::println);
    	data.put("RESULT", Result.SUCCESS);
    }
    
    @RequestView(uri="/toDeviceModelEdit", view="toDeviceModelEdit")
    @Dispatcher(success="/info/device_model_edit", failed="")
    private void toDeviceModelEdit(Map<String, Object[]> params, Map<String, Object> data) throws Exception {
    	Map<String, Object> param = getParameterMap(params);
    	// 根据ID获取信息
    	DeviceModel deviceModel = (DeviceModel) BaseDao.excuteQuery(param, DeviceModel.class);
    	data.put("RESULT", Result.SUCCESS);
    	data.put("deviceModel", deviceModel);
    }
    
    @RequestView(uri="/editDeviceModel", view="editDeviceModel")
    @ResponseJson
    private void editDeviceModel(Map<String, Object[]> params, Map<String, Object> data) throws Exception {
    	Map<String, Object> param = getParameterMap(params);
    	BaseDao.excute(param);
    	data.put("RESULT", Result.SUCCESS);
    	data.put("message", "操作成功");
    }
    
    
    // 去供应商页面
    @RequestView(uri="/toSupplier", view="toSupplier")
    @Dispatcher(success="/info/supplier", failed="")
    private void toSupplier(Map<String, Object[]> params, Map<String, Object> data) throws Exception {
    	Map<String, Object> param = getParameterMap(params);
    	List<Supplier> suppliers = (List<Supplier>) BaseDao.excuteQuery(param, Supplier.class);
    	data.put("suppliers", suppliers);
    	data.put("RESULT", Result.SUCCESS);
    }
    
    // 去供应商编辑页面
    @RequestView(uri="/toSupplierEdit", view="getSupplierById")
    @Dispatcher(success="/info/supplier_edit", failed="")
    private void toSupplierEdit(Map<String, Object[]> params, Map<String, Object> data) throws Exception {
    	Map<String, Object> param = getParameterMap(params);
    	Supplier supplier = (Supplier) BaseDao.excuteQuery(param, Supplier.class);
    	
    	data.put("RESULT", Result.SUCCESS);
    	data.put("supplier", supplier);
    	
    }
    
    // 编辑供应商
    @RequestView(uri="/editSupplier", view="editSupplier")
    @ResponseJson
    private void editSupplier(Map<String, Object[]> params, Map<String, Object> data) throws Exception {
    	Map<String, Object> param = getParameterMap(params);
    	if(param.get("type").toString().trim().equals("设备")) {
    		param.put("type", 1);
    	}else {
    		param.put("type", 0);
    	}
    	BaseDao.excute(param);
    	data.put("RESULT", Result.SUCCESS);
    	data.put("message", "操作成功");
    }
    
    /**
     * @throws Exception 
     * 
     */
    @RequestView(uri="/exportTea", view="exportTea")
    @ResponseFile
    private void exportTea(Map<String, Object[]> params, Map<String, Object> data) throws Exception {
    	Map<String, Object> param = getParameterMap(params);
    	List<PutAndDelivery> putAndDeliveries = (List<PutAndDelivery>) BaseDao.excuteQuery(param, PutAndDelivery.class, true);
    	// 进行封装对象， 然后生成文件
    	// 查询出入库记录
    	List<PutAndDeliveryVO>  putAndDeliveryVOs = new ArrayList<PutAndDeliveryVO>();
    	Map<String, Object> getUserById = new HashMap<String, Object>();
    	PutAndDeliveryVO putAndDeliveryVO = null;
    	for(int i = 0; i<putAndDeliveries.size();i++) {
    		putAndDeliveryVO = BeanUtil.convertBean(putAndDeliveries.get(i), PutAndDeliveryVO.class);
    		// 查询出我们的操作员信息
    		getUserById.put("userId", putAndDeliveries.get(i).getUserId());
    		getUserById.put("view", "getUserById");
    		//System.out.println(getUserById.get("view"));
    		User user = (User) BaseDao.excuteQuery(getUserById, User.class);
    		putAndDeliveryVO.setUser(user);
    		putAndDeliveryVOs.add(putAndDeliveryVO);
    	}
    	String title[] = {"批次", "数量", "出入库时间", "用户编号", "实时库存", "出入库类别", "茶叶名称", "采购批次"};
    	String[][] values = new String[putAndDeliveries.size()][8];
    	for(int i = 0; i < putAndDeliveries.size(); i++) {
    		values[i][0] = putAndDeliveries.get(i).getBatch();
    		values[i][1] = putAndDeliveries.get(i).getNumber().toString();
    		values[i][2] = putAndDeliveries.get(i).getPdTime();
    		values[i][3] = putAndDeliveries.get(i).getUserId().toString();
    		values[i][4] = putAndDeliveries.get(i).getRepertory().toString();
    		Integer pdType = putAndDeliveries.get(i).getPdType();
    		if(pdType == 1) {
    			values[i][5] = "入库";
    		}else {
    			values[i][5] = "出库";
    		}
    		values[i][6] = putAndDeliveries.get(i).getName();
    		values[i][7] = putAndDeliveries.get(i).getBuyBatch();
    	}
    	HSSFWorkbook wb = new HSSFWorkbook();
    	ExcelUtil.getHSSFWorkbook("tea", title, values, wb);
    	data.put("RESULT", Result.SUCCESS);
    	data.put("wb", wb);
    }
    
    
    @RequestView(uri="/exportDevice", view="exportDevice")
    @ResponseFile
    private void exportDevice(Map<String, Object[]> params, Map<String, Object> data) throws Exception {
    	Map<String, Object> param = getParameterMap(params);
    	List<PutAndDelivery> putAndDeliveries = (List<PutAndDelivery>) BaseDao.excuteQuery(param, PutAndDelivery.class, true);
    	// 进行封装对象， 然后生成文件
    	// 查询出入库记录
    	List<PutAndDeliveryVO>  putAndDeliveryVOs = new ArrayList<PutAndDeliveryVO>();
    	Map<String, Object> getUserById = new HashMap<String, Object>();
    	PutAndDeliveryVO putAndDeliveryVO = null;
    	for(int i = 0; i<putAndDeliveries.size();i++) {
    		putAndDeliveryVO = BeanUtil.convertBean(putAndDeliveries.get(i), PutAndDeliveryVO.class);
    		// 查询出我们的操作员信息
    		getUserById.put("userId", putAndDeliveries.get(i).getUserId());
    		getUserById.put("view", "getUserById");
    		//System.out.println(getUserById.get("view"));
    		User user = (User) BaseDao.excuteQuery(getUserById, User.class);
    		putAndDeliveryVO.setUser(user);
    		putAndDeliveryVOs.add(putAndDeliveryVO);
    	}
    	String title[] = {"批次", "数量", "出入库时间", "用户编号", "实时库存", "出入库类别", "机器名称", "采购批次"};
    	String[][] values = new String[putAndDeliveries.size()][8];
    	for(int i = 0; i < putAndDeliveries.size(); i++) {
    		values[i][0] = putAndDeliveries.get(i).getBatch();
    		values[i][1] = putAndDeliveries.get(i).getNumber().toString();
    		values[i][2] = putAndDeliveries.get(i).getPdTime();
    		values[i][3] = putAndDeliveries.get(i).getUserId().toString();
    		values[i][4] = putAndDeliveries.get(i).getRepertory().toString();
    		Integer pdType = putAndDeliveries.get(i).getPdType();
    		if(pdType == 1) {
    			values[i][5] = "入库";
    		}else {
    			values[i][5] = "出库";
    		}
    		values[i][6] = putAndDeliveries.get(i).getName();
    		values[i][7] = putAndDeliveries.get(i).getBuyBatch();
    	}
    	HSSFWorkbook wb = new HSSFWorkbook();
    	ExcelUtil.getHSSFWorkbook("device", title, values, wb);
    	data.put("RESULT", Result.SUCCESS);
    	data.put("wb", wb);
    }
    
    
    
    

}
